package cn.ds.assets.module.assets.dal.mysql.metadataDatasource;

import java.util.*;
import java.time.LocalDateTime;
import java.time.LocalDate;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ds.assets.framework.mybatis.core.mapper.BaseMapperX;
import cn.ds.assets.module.assets.dal.dataobject.metadataDatasource.BaseMetadataDatasourceDO;
import org.apache.ibatis.annotations.Mapper;
import cn.ds.assets.module.assets.controller.admin.metadataDatasource.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 元数据管理-数据源 Mapper
 *
 * @author admin
 */
@Mapper
public interface BaseMetadataDatasourceMapper extends BaseMapperX<BaseMetadataDatasourceDO> {

    default PageResult<BaseMetadataDatasourceDO> selectPage(BaseMetadataDatasourcePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BaseMetadataDatasourceDO>()
                .likeIfPresent(BaseMetadataDatasourceDO::getName, reqVO.getName())
                .eqIfPresent(BaseMetadataDatasourceDO::getUrl, reqVO.getUrl())
                .likeIfPresent(BaseMetadataDatasourceDO::getUsername, reqVO.getUsername())
                .eqIfPresent(BaseMetadataDatasourceDO::getPassword, reqVO.getPassword())
                .eqIfPresent(BaseMetadataDatasourceDO::getDesc, reqVO.getDesc())
                .eqIfPresent(BaseMetadataDatasourceDO::getDataLayer, reqVO.getDataLayer())
                .eqIfPresent(BaseMetadataDatasourceDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(BaseMetadataDatasourceDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BaseMetadataDatasourceDO::getProjectId, reqVO.getProjectId())
                .eqIfPresent(BaseMetadataDatasourceDO::getType, reqVO.getType())
                .inIfPresent(BaseMetadataDatasourceDO::getId,reqVO.getIds())
                .orderByDesc(BaseMetadataDatasourceDO::getId));
    }

    default List<BaseMetadataDatasourceDO> selectList(BaseMetadataDatasourceExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<BaseMetadataDatasourceDO>()
                .likeIfPresent(BaseMetadataDatasourceDO::getName, reqVO.getName())
                .eqIfPresent(BaseMetadataDatasourceDO::getUrl, reqVO.getUrl())
                .likeIfPresent(BaseMetadataDatasourceDO::getUsername, reqVO.getUsername())
                .eqIfPresent(BaseMetadataDatasourceDO::getPassword, reqVO.getPassword())
                .eqIfPresent(BaseMetadataDatasourceDO::getDesc, reqVO.getDesc())
                .eqIfPresent(BaseMetadataDatasourceDO::getDataLayer, reqVO.getDataLayer())
                .eqIfPresent(BaseMetadataDatasourceDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(BaseMetadataDatasourceDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BaseMetadataDatasourceDO::getProjectId, reqVO.getProjectId())
                .eqIfPresent(BaseMetadataDatasourceDO::getType, reqVO.getType())
                .inIfPresent(BaseMetadataDatasourceDO::getId,reqVO.getIds())
                .orderByDesc(BaseMetadataDatasourceDO::getId));
    }

    @Update("UPDATE assets_metadata_datasource SET deleted = 1, update_time = #{deletedTime} WHERE id = #{id}")
    void updateToDelete(@Param("id") Long id, @Param("deletedTime") LocalDateTime deletedTime);
}
